Imports Microsoft.VisualBasic
Imports System.xml
Imports System.web.httpcontext

Public Class XmlManipulator
    Implements IDataStore

    Private _filePath As String
    Private Const FILEPATH_BASE As String = "~/App_Data/"
    Private Const FILE_EXTENSION As String = ".xml"
    Private Const USERS_FILENAME As String = "users"

    Private _pageName As String
    Private _contentKey As String

    Private _context As HttpContext

    Public Sub New(ByVal page_name As String)

        _pageName = page_name

    End Sub

    Public Property ContentKey() As String Implements IDataStore.ContentKey
        Get
            Return _contentKey
        End Get
        Set(ByVal value As String)
            _contentKey = value.ToLower
        End Set
    End Property

    Private Function buildPath(ByVal fileName As String) As String

        _context = HttpContext.Current
        Dim fileString As String = FILEPATH_BASE
        fileString &= fileName
        fileString &= FILE_EXTENSION

        _filePath = _context.Server.MapPath(fileString)
        Return _filePath

    End Function

    Public Function GetContent(ByVal VersionID As Integer) As String _
        Implements IDataStore.GetContent

        Dim versionType As String
        Select Case VersionID
            Case 0
                versionType = "live"
            Case 1
                versionType = "staged"
            Case Else
                versionType = "not defined"
        End Select

        Dim contentReader As New XmlTextReader(buildPath(_pageName))
        Dim contentDoc As New XmlDocument

        contentDoc.Load(contentReader)
        contentReader.Close()

        Dim nodeList As XmlNodeList
        nodeList = contentDoc.GetElementsByTagName(ContentKey)

        For Each n As XmlNode In nodeList
            If Not n.Item(versionType) Is Nothing Then
                If Not n.Item(versionType).FirstChild Is Nothing Then

                    Return n.Item(versionType).FirstChild.Value

                Else

                    Return ""

                End If

            End If
        Next

        Return "No XML element for this content control."

    End Function

    Public Sub PublishContent() _
        Implements IDataStore.PublishContent

        Dim filePath As String
        filePath = buildPath(_pageName)

        Dim contentReader As New XmlTextReader(filePath)
        Dim contentDoc As New XmlDocument

        contentDoc.Load(contentReader)
        contentReader.Close()

        Dim nodeList As XmlNodeList
        nodeList = contentDoc.GetElementsByTagName(ContentKey)

        For Each n As XmlNode In nodeList

            Dim textValue As String = ""

            If Not n.Item("staged").FirstChild Is Nothing Then
                If Not n.Item("staged").FirstChild.Value Is Nothing Then

                    textValue = _
                        n.Item("staged").FirstChild.Value
                Else
                    textValue = ""
                End If
                If n.Item("live").FirstChild Is Nothing Then
                    n.Item("live").AppendChild(contentDoc.CreateNode(XmlNodeType.Text, "", ""))
                End If
            End If

            If n.Item("live").FirstChild Is Nothing Then
                n.Item("live").AppendChild(contentDoc.CreateNode(XmlNodeType.Text, "", ""))
            End If

            Dim finalTextValue As New StringBuilder
            With finalTextValue
                .Append("<p>")
                .Append(textValue)
                .Append("</p>")
            End With

            n.Item("live").FirstChild.Value = textValue
            contentDoc.Save(filePath)

        Next

    End Sub

    Public Sub SaveContent(ByVal Text As String) _
        Implements IDataStore.SaveContent



        Dim contentReader As New XmlTextReader(buildPath(_pageName))
        Dim contentDoc As New XmlDocument

        contentDoc.Load(contentReader)
        contentReader.Close()

        Dim nodeList As XmlNodeList
        nodeList = contentDoc.GetElementsByTagName(ContentKey)

        For Each n As XmlNode In nodeList
            If n.Item("staged").FirstChild Is Nothing Then
                n.Item("staged").AppendChild(contentDoc.CreateNode(XmlNodeType.Text, "", ""))
            End If
            If Not n.Item("staged") Is Nothing Then
                n.Item("staged").FirstChild.Value = Text
                contentDoc.Save(buildPath(_pageName))
            End If
        Next

    End Sub

    Public Function ValidateUser(ByVal UserName As String, _
        ByVal Password As String) As String _
        Implements IDataStore.ValidateUser

        Dim uName As String
        Dim pWord As String

        Dim usersDoc As New XmlTextReader(buildPath(USERS_FILENAME))
        While (usersDoc.Read())
            If usersDoc.NodeType = XmlNodeType.Element Then

                Select Case usersDoc.Name
                    Case "userName"
                        usersDoc.ReadStartElement()
                        uName = usersDoc.Value
                    Case "password"
                        usersDoc.ReadStartElement()
                        pWord = usersDoc.Value
                End Select
                If uName = UserName AndAlso pWord = Password Then
                    Return uName
                    usersDoc.Close()
                    Exit Function
                End If
            End If
        End While
        Return ""
        usersDoc.Close()

    End Function

End Class
